package com.etop.ondoc.beans;

import java.util.Date;
import java.util.Objects;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

/**
 * 用户与文档的浏览下载收藏(1为下载，2为收藏，3为浏览)关系的多对多关联<br/>
 * 用这个关联表转换成两个一对多的关联<br/>
 * 如果用户需要加载他浏览过的文档，只需要查type=3的这个关系即可（非懒加载）<br/>
 * <p>注意，如果自己写sql，最好把它的User、Document关联对象一起取出来</p>
 * <p>设置了表示关系、记录是否有效的常量</p>
 * 也可以用三张表，分别保存浏览、下载、收藏的关系，这样dao层就简单的多
 *
 * @author guin
 *
 */
public class UserDoc {
    
    public static final int TYPE_DOWNLOAD = 1;
    public static final int TYPE_COLLECT = 2;
    public static final int TYPE_VIEW = 3;
    
    public static final int IS_VAILD = 1;
    public static final int IS_NOT_VAILD = 0;
    
    
    private int id;
    private User user;
    private Document document;
    /**
     * 1为下载，2为收藏，3为浏览
     */
    private int type;
    /**
     * 1：有效，0：无效
     */
    private int isValid;
    private Date opeTime;

    /**
     * isVaild属性已初始化，<br/>
     * 其他属性都要初始化<br/>
     */
    public UserDoc() {
        this.isValid = IS_VAILD;
    }

    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

	/**
     * 因为用户获取相应的列表，所以懒加载
     *
     * @return
     */
    @ManyToOne(fetch = FetchType.LAZY, targetEntity = User.class)
    @JoinColumn(name = "u_id")
    @Cascade(CascadeType.SAVE_UPDATE)
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    /**
     * 因为用户获取列表，所以自动加载该关系对应的文档，不懒加载
     *
     * @return
     */
    @ManyToOne(targetEntity = Document.class)
    @JoinColumn(name = "d_id")
    @Cascade(CascadeType.SAVE_UPDATE)
    @Fetch(FetchMode.JOIN)
    public Document getDocument() {
        return document;
    }

    public void setDocument(Document document) {
        this.document = document;
    }

    /**
     * 1为下载，2为收藏，3为浏览
     */
    @Column(name = "type")
    public int getType() {
        return type;
    }

    /**
     * 1为下载，2为收藏，3为浏览
     */
    public void setType(int type) {
        this.type = type;
    }

    /**
     * 1：有效，0：无效
     */
    @Column(name = "is_vaild")
    public int getIsValid() {
        return isValid;
    }

    /**
     * 1：有效，0：无效
     */
    public void setIsValid(int isValid) {
        this.isValid = isValid;
    }

    @Column(name = "ope_time")
    @Temporal(value = TemporalType.TIMESTAMP)
    public Date getOpeTime() {
        return opeTime;
    }

    public void setOpeTime(Date opeTime) {
        this.opeTime = opeTime;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 19 * hash + Objects.hashCode(this.document);
        hash = 19 * hash + this.type;
        hash = 19 * hash + Objects.hashCode(this.opeTime);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final UserDoc other = (UserDoc) obj;
        if (!Objects.equals(this.document, other.document)) {
            return false;
        }
        if (this.type != other.type) {
            return false;
        }
        if (!Objects.equals(this.opeTime, other.opeTime)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        String s = null;
        try{
            s = "UserDoc{" + "id=" + id + ", document=" + document.getName() + ", type=" + type + ", isValid=" + isValid + ", opeTime=" + opeTime + '}';
        } catch(Exception e){
            System.err.println("UserDoc实体类的toString错误，请查看源码");
        }
        return s;
    }

}
